package com.ibm.parse.controller;

import com.ibm.parse.service.TextParseService;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings("rawtypes")
@Controller
public class HomeController {

	@Autowired
	private TextParseService textParseService;

	@RequestMapping(value = { "/" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView home(Locale locale, Model model) {
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(1, 1, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate);
		model.toString();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("home");
		return mav;
	}
	
	@RequestMapping(value = { "/a" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public ModelAndView getProducts(String des) {
		ModelAndView mav = new ModelAndView();
		List list = new ArrayList();
		list = this.textParseService.parseText(des);
		JSONArray json = JSONArray.fromObject(list);
		mav.addObject("list", json.toString());
		mav.setViewName("result");
		return mav;
	}

	@RequestMapping(value = { "/analysis" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String getProduct(String des) {
		System.out.println(des);
		List list = new ArrayList();
		list = this.textParseService.parseText2(des);
		JSONArray json = JSONArray.fromObject(list);
		return json.toString();
	}

	@RequestMapping(value = { "/findKeyword" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findKeyword(Locale locale, Model model) {
		List list = new ArrayList();
		list = this.textParseService.findKeywordList();
		JSONArray json = JSONArray.fromObject(list);
		return json.toString();
	}

	@RequestMapping(value = { "/findKindIdentifierList" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findKindIdentifier(Locale locale, Model model) {
		List list = new ArrayList();
		list = this.textParseService.findKindIdentifierList();
		JSONArray json = JSONArray.fromObject(list);
		return json.toString();
	}

	@RequestMapping(value = { "/findKeywordInMermory" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findKeywordInMermory(Locale locale, Model model) {
		Map map = this.textParseService.getMap();
		JSONArray json = JSONArray.fromObject(map);
		return json.toString();
	}

	@RequestMapping(value = { "/findKindIdentifierListInMermory" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findKindIdentifierInMermory(Locale locale, Model model) {
		List list = this.textParseService.getIdentifierList();
		JSONArray json = JSONArray.fromObject(list);
		return json.toString();
	}

	@RequestMapping(value = { "/update" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String update(Locale locale, Model model, String keywords) {
		int result = this.textParseService.update(keywords);
		return String.valueOf(result);
	}

	@RequestMapping(value = { "/updateKeyword" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String updateKeyword(Locale locale, Model model) {
		this.textParseService.updateKeyword();
		return "1";
	}

	@RequestMapping(value = { "/updateKindIdentifier" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String updateIdentifier(Locale locale, Model model) {
		this.textParseService.updateKindIdentifier();
		return "1";
	}

	@RequestMapping(value = { "/welcome" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView test(String name) {
		ModelAndView mav = new ModelAndView();

		mav.setViewName("test");
		return mav;
	}
}